package swimv2gm.client.requests;

import java.util.*;
import java.io.IOException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2gm.bl.session.WorkRequestManager;

/**
 * Servlet implementation class ShowRequest
 */
public class ShowRequest extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		WorkRequestManager man;
		try {
			man = (WorkRequestManager)InitialContext.doLookup("WorkRequestManager");
		} catch (NamingException e) {
			response.sendError(500);
			return;
		}
		Long uid = (Long)request.getSession().getAttribute("user");

		if (uid == null) {
			response.sendError(403);
			return;
		}

		String rid_p = request.getParameter("rid");
		long rid = -1;
		try {
			rid = Long.parseLong(rid_p);
		} catch (IllegalArgumentException e) {
			request.getRequestDispatcher("showRequest.jsp?error=true").forward(request,response);
			return;
		}
		Map<String,String> data = man.getRequestData(rid);

		if (data != null) {
			if (!uid.toString().equals(data.get("mitt_id")) && !uid.toString().equals(data.get("dest_id"))) {
				response.sendError(403);
				return;
			}
		}

		request.setAttribute("data",data);

		request.getRequestDispatcher("showRequest.jsp").forward(request,response);
	}

	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException {
		doGet(request,response);
	}

}
